package com.xuxu.product.controller;

import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.xuxu.product.dao.ProductDao;
import com.xuxu.product.service.impl.ElasticServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.xuxu.product.entity.ProductEntity;
import com.xuxu.product.service.ProductService;
import com.xuxu.common.utils.PageUtils;
import com.xuxu.common.utils.R;

/**
 * @author maxuxu
 * @email maxuxu019@gmail.com
 * @date 2022-08-17 18:08:13
 */
@RestController
@RequestMapping("product/product")
@Slf4j
public class ProductController {
    @Autowired
    private ProductService productService;
    @Autowired
    private ProductDao productDao;
    @Autowired
    ElasticServiceImpl elasticService;

    @RequestMapping("/insert")
    public R insert() throws IOException {
        ProductEntity productEntity = new ProductEntity();
        productEntity.setPid("1");
        productEntity.setPname("java入门到入狱");
        productEntity.setPics("/img/java.jpg");
        productEntity.setPview(1);
        productEntity.setPbody("书很好，收获很大");
        productService.save(productEntity);
        elasticService.addDocum(productEntity, "2");
        return R.ok();
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = productService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        ProductEntity product = productService.getById(id);
        return R.ok().put("product", product);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody ProductEntity product) throws IOException {
        productService.save(product);
        elasticService.addDocum(product, product.getPid());
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody ProductEntity product) {
        productService.updateById(product);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        productService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

    /**
     * 查询所有用户
     */
    @RequestMapping("/findall/{name}/{index}/{size}")
    public R findAll(@PathVariable("name") String name,
                     @PathVariable("index") Integer index, @PathVariable("size") Integer size) throws IOException {
        List<ProductEntity> all = productDao.selectList(null);
        elasticService.searchPage(name, index, size);
        List<Map<String, Object>> maps = elasticService.highLightSearch(name, index, size);
        return R.ok().put("products", maps);
    }
}
